<?php


namespace app\admin\service;


use app\common\library\helper;
use think\Model;

class User extends Model
{
    protected $name='user';

    /**
     * 获取当前用户总数
     * @param null $day
     * @return int|string
     * @throws \think\Exception
     */
    public function getUserTotal($day = null)
    {
        if (!is_null($day)) {
            $startTime = strtotime($day);
            $this->where('create_time', '>=', $startTime)
                ->where('create_time', '<', $startTime + 86400);
        }
        return $this->where('is_delete', '=', '0')->count();
    }

    /**
     * 获取本日新增
     * @param $day
     * @return int|string
     * @throws \think\Exception
     */
    public function getUserTodayTotal($day){
        return $this->where('is_delete', '=', 0)->whereTime('create_time',$day)->count();
    }

    /**
     *  获取本月新增
     * @param $month
     * @return int|string
     * @throws \think\Exception
     */

    public function getUserMonthTotal($month){
        return $this->where('is_delete', '=', 0)->whereTime('create_time', $month)->count();
    }

    /**
     * 获取用户总量
     * @param null $startDate
     * @param null $endDate
     * @return string
     * @throws \think\Exception
     */
    public function getUserTimeTotal($startDate = null, $endDate = null)
    {

        if (!is_null($startDate) && !is_null($endDate)) {
            $this->where('create_time', '>=', strtotime($startDate))
                ->where('create_time', '<', strtotime($endDate) + 86400);
        }
        $value =  $this->where('is_delete', '=', '0')->count();
        return number_format($value);
    }

    /**
     * 用户充值总额
     * @param null $startDate
     * @param null $endDate
     * @return float|int
     */
    public function getRechargeTotal($startDate = null, $endDate = null)
    {
        if (!is_null($startDate) && !is_null($endDate)) {
            $value =db('recharge_order')->where('pay_time', '>=', strtotime($startDate))
                ->where('pay_time', '<', strtotime($endDate) + 86400)
                ->where('pay_status', '=', 20)
                ->sum('actual_money');
        }else{
            $value =db('recharge_order')->where('pay_status', '=', 20)
                ->sum('actual_money');
        }

        return helper::number2($value);
    }
    /**
     * 用户消费榜
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function geUserExpendRanking()
    {
        return $this->field(['user_id', 'nickName', 'expend_money'])
            ->where('is_delete', '=', 0)
            ->order(['expend_money' => 'DESC'])
            ->limit(10)
            ->select();
    }
}